package com.api.shopapi.controller;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.extra.validation.ValidationUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.config.RoleConfig;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.MedicinerDTO;
import com.api.shopapi.entity.dto.MedicinerStaffRelDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 医生
 */
@RestController
@RequestMapping("Mediciner")
public class MedicinerController extends BaseController {

    @Autowired
    @Qualifier("MedicinerServiceImplOne")
    private MedicinerService medicinerService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @Autowired
    @Qualifier("RoleServiceImplOne")
    private RoleService roleService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;


    @Autowired
    @Qualifier("MedicinerStaffRelServiceImplOne")
    private MedicinerStaffRelService medicinerStaffRelService;


    @Autowired
    @Qualifier("InstitutionServiceImplOne")
    private InstitutionService institutionService;


    @Autowired
    @Qualifier("UserFaceServiceImplOne")
    private UserFaceService userFaceService;


    //政府管理员获取面板数据
    @RequestMapping(value = "PannelVisual", method = RequestMethod.POST)
    public MyResult PannelVisual(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String governmentCode = jsonObject.getString("governmentCode");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign, "/Mediciner/PannelVisual");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<MedicinerDTO> list = medicinerService.listByGovCode(governmentCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("medicinerCount", list.size());
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Mediciner/PannelVisual", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //ins
    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String loginName = jsonObject.getString("loginName");
            String password = jsonObject.getString("password");
            String name = jsonObject.getString("name");
            String phone = jsonObject.getString("phone");
            String idCard = jsonObject.getString("idCard");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode, "/Mediciner/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            Staff staff = staffService.getByStaffCode(institutionStaffRel.getStaffCode());

            User user = userService.getByLoginName(loginName);
            if (user != null) {
                return failed("登陆名已注册");
            }

            user = new User();
            String userCode = UUID.randomUUID().toString();
            user.setUserCode(userCode);
            user.setLoginName(loginName);
            user.setPhone(phone);
            user.setName(name);
            user.setPassword(getMD5Str(password));
            user.setCreateTime(new Date());
            user.setUserType(7);
            user.setStatus(1);
            user.setInstitutionCode(institutionCode);
            user.setGovernmentCode(staff.getGovernmentCode());


            Mediciner mediciner = new Mediciner();
            String medicinerCode = UUID.randomUUID().toString();
            mediciner.setMedicinerCode(medicinerCode);
            mediciner.setUserCode(userCode);
            mediciner.setIdCard(idCard);
            mediciner.setInstitutionCode(institutionCode);
            medicinerService.regInsert(user, mediciner);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/Mediciner/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult update(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String medicinerCode = jsonObject.getString("medicinerCode");

            String name = jsonObject.getString("name");
            String phone = jsonObject.getString("phone");
            int status = jsonObject.getInteger("status");
            String idCard = jsonObject.getString("idCard");


            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode, "/Mediciner/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            Mediciner mediciner = medicinerService.getByMedicinerCode(medicinerCode);
            if (mediciner == null) {
                return failed("数据不存在");
            }

            User user = userService.getByUserCode(mediciner.getUserCode());
            user.setName(name);
            user.setPhone(phone);
            user.setStatus(status);

            mediciner.setIdCard(idCard);
            medicinerService.updateByMedicinerCode(user, mediciner);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/Mediciner/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins
    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode, "/Mediciner/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<MedicinerDTO> pageInfo = medicinerService.getPageList(pageIndex, pageSize, institutionCode, name, status);

            for (MedicinerDTO medicinerDTO : pageInfo.getList()) {
                User user = userService.getByUserCode(medicinerDTO.getUserCode());
                medicinerDTO.setName(user.getName());
                medicinerDTO.setPhone(user.getPhone());
                medicinerDTO.setStatus(user.getStatus());
                medicinerDTO.setLoginName(user.getLoginName());
                medicinerDTO.setCreateTime(user.getCreateTime());

                List<MedicinerStaffRelDTO> listDTO =
                        medicinerStaffRelService.getListByMedicinerCode(medicinerDTO.getMedicinerCode());
                for (MedicinerStaffRelDTO medicinerStaffRelDTO : listDTO) {

                    Mediciner mediciner = medicinerService.getByMedicinerCode(medicinerStaffRelDTO.getMedicinerCode());
                    User user2 = userService.getByUserCode(mediciner.getUserCode());
                    medicinerStaffRelDTO.setMedicinerName(user2.getName());

                    Staff staff1 = staffService.getByStaffCode(medicinerStaffRelDTO.getStaffCode());
                    medicinerStaffRelDTO.setStaffName(staff1.getLoginName());

                }
                medicinerDTO.setListMedicinerStaffRelDTO(listDTO);

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo", pageInfo);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Mediciner/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins
    @RequestMapping(value = "InsPageByInsCode", method = RequestMethod.POST)
    public MyResult InsPageByInsCode(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String institutionCode = jsonObject.getString("institutionCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign, "/Mediciner/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<MedicinerDTO> pageInfo = medicinerService.getPageList(pageIndex, pageSize, institutionCode, "", -1);

            for (MedicinerDTO medicinerDTO : pageInfo.getList()) {
                User user = userService.getByUserCode(medicinerDTO.getUserCode());
                medicinerDTO.setName(user.getName());
                medicinerDTO.setPhone(user.getPhone());
                medicinerDTO.setStatus(user.getStatus());
                medicinerDTO.setLoginName(user.getLoginName());
                medicinerDTO.setCreateTime(user.getCreateTime());

                List<MedicinerStaffRelDTO> listDTO =
                        medicinerStaffRelService.getListByMedicinerCode(medicinerDTO.getMedicinerCode());
                for (MedicinerStaffRelDTO medicinerStaffRelDTO : listDTO) {

                    Mediciner mediciner = medicinerService.getByMedicinerCode(medicinerStaffRelDTO.getMedicinerCode());
                    User user2 = userService.getByUserCode(mediciner.getUserCode());
                    medicinerStaffRelDTO.setMedicinerName(user2.getName());

                    Staff staff1 = staffService.getByStaffCode(medicinerStaffRelDTO.getStaffCode());
                    medicinerStaffRelDTO.setStaffName(staff1.getLoginName());

                }
                medicinerDTO.setListMedicinerStaffRelDTO(listDTO);

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo", pageInfo);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Mediciner/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins
    @RequestMapping(value = "InsPageApp", method = RequestMethod.POST)
    public MyResult InsPageApp(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign, "/Mediciner/InsPageApp");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Institution institution = institutionService.getByUserCode(userCode);
            String institutionCode = institution.getInstitutionCode();

            PageInfo<MedicinerDTO> pageInfo = medicinerService.getPageList(pageIndex, pageSize, institutionCode, name, status);

            for (MedicinerDTO medicinerDTO : pageInfo.getList()) {
                User user = userService.getByUserCode(medicinerDTO.getUserCode());
                medicinerDTO.setName(user.getName());
                medicinerDTO.setPhone(user.getPhone());
                medicinerDTO.setStatus(user.getStatus());
                medicinerDTO.setLoginName(user.getLoginName());
                medicinerDTO.setCreateTime(user.getCreateTime());

                UserFace userFace = userFaceService.getByUserCode(medicinerDTO.getUserCode());
                if (userFace == null) {
                    medicinerDTO.setIsRegFace(false);
                } else {
                    medicinerDTO.setIsRegFace(true);
                }

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo", pageInfo);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Mediciner/InsPageApp", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins
    @RequestMapping(value = "InsDelete", method = RequestMethod.POST)
    public MyResult InsDelete(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode, "/Mediciner/InsDelete");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            List<Mediciner> list = medicinerService.listByInsCode(institutionCode);
            for (Mediciner mediciner : list) {

                medicinerService.deleteByMedicinerCode(mediciner.getMedicinerCode());

                //删除老的关系
                List<MedicinerStaffRelDTO> listStaffRel = medicinerStaffRelService.getListByMedicinerCode(mediciner.getMedicinerCode());
                for (MedicinerStaffRelDTO medicinerStaffRelDTO : listStaffRel) {
                    medicinerStaffRelService.delete(medicinerStaffRelDTO.getMedicinerStaffRelCode());
                }

            }


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Mediciner/InsDelete", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //gov
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode, "/Mediciner/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<MedicinerDTO> pageInfo = medicinerService.pageByGovCode(pageIndex, pageSize, staff.getGovernmentCode());

            for (MedicinerDTO medicinerDTO : pageInfo.getList()) {
                User user = userService.getByUserCode(medicinerDTO.getUserCode());
                medicinerDTO.setName(user.getName());
                medicinerDTO.setPhone(user.getPhone());
                medicinerDTO.setStatus(user.getStatus());
                medicinerDTO.setLoginName(user.getLoginName());
                medicinerDTO.setCreateTime(user.getCreateTime());

                List<MedicinerStaffRelDTO> listDTO =
                        medicinerStaffRelService.getListByMedicinerCode(medicinerDTO.getMedicinerCode());
                for (MedicinerStaffRelDTO medicinerStaffRelDTO : listDTO) {

                    Mediciner mediciner = medicinerService.getByMedicinerCode(medicinerStaffRelDTO.getMedicinerCode());
                    User user2 = userService.getByUserCode(mediciner.getUserCode());
                    medicinerStaffRelDTO.setMedicinerName(user2.getName());

                    Staff staff1 = staffService.getByStaffCode(medicinerStaffRelDTO.getStaffCode());
                    medicinerStaffRelDTO.setStaffName(staff1.getLoginName());

                }
                medicinerDTO.setListMedicinerStaffRelDTO(listDTO);

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo", pageInfo);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Mediciner/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "GovPage2", method = RequestMethod.POST)
    public MyResult GovPage2(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String institutionCode = jsonObject.getString("institutionCode");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode, "/Mediciner/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<Mediciner> pageInfo = medicinerService.pageByGovCodeInsCode(pageIndex, pageSize,
                    staff.getGovernmentCode(), institutionCode);

            List<MedicinerDTO> listDTO2 = new ArrayList<>();
            for (Mediciner mediciner2 : pageInfo.getList()) {

                mediciner2 = medicinerService.getByMedicinerCode(mediciner2.getMedicinerCode());
                MedicinerDTO medicinerDTO = mapper.map(mediciner2, MedicinerDTO.class);

                User user = userService.getByUserCode(medicinerDTO.getUserCode());
                medicinerDTO.setName(user.getName());
                medicinerDTO.setPhone(user.getPhone());
                medicinerDTO.setStatus(user.getStatus());
                medicinerDTO.setLoginName(user.getLoginName());
                medicinerDTO.setCreateTime(user.getCreateTime());

                List<MedicinerStaffRelDTO> listDTO =
                        medicinerStaffRelService.getListByMedicinerCode(medicinerDTO.getMedicinerCode());
                for (MedicinerStaffRelDTO medicinerStaffRelDTO : listDTO) {

                    Mediciner mediciner = medicinerService.getByMedicinerCode(medicinerStaffRelDTO.getMedicinerCode());
                    User user2 = userService.getByUserCode(mediciner.getUserCode());
                    medicinerStaffRelDTO.setMedicinerName(user2.getName());

                    Staff staff1 = staffService.getByStaffCode(medicinerStaffRelDTO.getStaffCode());
                    medicinerStaffRelDTO.setStaffName(staff1.getLoginName());

                }
                medicinerDTO.setListMedicinerStaffRelDTO(listDTO);

                listDTO2.add(medicinerDTO);

            }

            pageInfo.setList(Collections.unmodifiableList(listDTO2));

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo", pageInfo);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Mediciner/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult List(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode, "/Mediciner/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            List<MedicinerDTO> medicinerDTOList = medicinerService.getList(institutionCode);
            return success(medicinerDTOList);
        } catch (Exception e) {
            insertError("/Mediciner/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "AddForOut", method = RequestMethod.POST)
    public MyResult AddForOut(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            JSONArray medArray = jsonObject.getJSONArray("medArray");
            String institutionCode = jsonObject.getString("institutionCode");

            if (!checkSignForOut(timeStr, sign)) {
                return failed("签名错误");
            }

            Institution institution = institutionService.getByInstitutionCode(institutionCode);
            Role role = roleService.getByName(RoleConfig.medicinerRoleName);


            HashMap<String, String> hashMapPhone = new HashMap<>();
            HashMap<String, String> hashMapIdCard = new HashMap<>();
            for (int i = 0; i < medArray.size(); i++) {

                String name = medArray.getJSONObject(i).getString("name").trim();
                String phone = medArray.getJSONObject(i).getString("phone").trim();
                String idCard = medArray.getJSONObject(i).getString("idCard").trim();

                if (!phone.equals("") && phone.length() != 11) {
                    return failed("上传数据中手机号格式错误" + phone);
                }
                if (phone.equals("")) {
                    phone = getPhoneAuto(hashMapPhone);
                }

                if (hashMapPhone.containsKey(phone)) {
                    return failed("上传数据中手机号存在重复的" + phone);
                } else {
                    hashMapPhone.put(phone, phone);
                }
                if (hashMapIdCard.containsKey(idCard)) {
                    return failed("上传数据中身份证号存在重复的" + idCard);
                } else {
                    hashMapIdCard.put(idCard, phone);
                }

                if (name.equals("")) {
                    return failed("上传数据中名称不能为空");
                }
                if (!isPhone(phone)) {
                    return failed("上传数据中手机格式错误");
                }

                if (!IdcardUtil.isValidCard(idCard)) {
                    return failed("上传数据中身份证格式错误");
                }
//                User user = userService.getByPhone(phone);
//                if (user != null) {
//                    return failed("手机号已使用：" + phone);
//                }

            }

            Date date = new Date();
            for (int i = 0; i < medArray.size(); i++) {

                String name = medArray.getJSONObject(i).getString("name");
                //String phone = medArray.getJSONObject(i).getString("phone");
                String idCard = medArray.getJSONObject(i).getString("idCard");

                String phone = hashMapIdCard.get(idCard);

                User user = userService.getByPhone(phone);

                if (user == null) {  //不存在 都要增加
                    user = new User();
                    user.setUserCode(UUID.randomUUID().toString());
                    user.setName(name);
                    user.setLoginName(phone);
                    user.setPhone(phone);
                    user.setPassword(getMD5Str(phone.substring(5, 11)));
                    user.setUserType(7);
                    user.setStatus(1);
                    user.setGovernmentCode(institution.getGovernmentCode());
                    user.setInstitutionCode(institutionCode);
                    user.setCreateTime(date);
                    userService.insert(user);


                    Mediciner mediciner = new Mediciner();
                    mediciner.setMedicinerCode(UUID.randomUUID().toString());
                    mediciner.setUserCode(user.getUserCode());
                    mediciner.setIdCard(idCard);
                    mediciner.setInstitutionCode(institutionCode);
                    mediciner.setCreateTime(date);
                    medicinerService.insert(mediciner);
                }else{
                    user.setName(name);
                    user.setLoginName(phone);
                    user.setPhone(phone);
                    user.setGovernmentCode(institution.getGovernmentCode());
                    user.setInstitutionCode(institutionCode);
                    userService.updateName(user);
                    Mediciner mediciner = medicinerService.getByUserCode(user.getUserCode());
                    mediciner.setIdCard(idCard);
                    mediciner.setInstitutionCode(institutionCode);
                    medicinerService.updateIdCardAndInstitution(mediciner);

                }


            }

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);

        } catch (Exception e) {
            insertError("/Mediciner/AddForOut", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //生成登陆手机号
    private String getPhoneAuto(HashMap<String, String> hashMapPhone) {

        String newPhone = "100" + random8();

        if (hashMapPhone.containsKey(newPhone)) {
            newPhone = getPhoneAuto(hashMapPhone);
        }

        User user = userService.getByPhone(newPhone);
        if (user != null) {
            newPhone = getPhoneAuto(hashMapPhone);
        }

        return newPhone;
    }

    /**
     * * 生成8位随机数
     **/
    private int random8() {
        Random r = new Random();
        StringBuffer sb = new StringBuffer();
        int[] c = new int[8];
        for (int i = 0; i < 8; i++) {
            c[i] = r.nextInt(9) + 1;
            sb.append(c[i]);
        }
        return Integer.parseInt(sb.toString());
    }


}
